﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace FileSharer
{
    public class FileSharerConfiguration
    {
        public static FileSharerConfiguration Load(string filename)
        {
            using (StreamReader stream = new StreamReader(filename))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(FileSharerConfiguration));
                FileSharerConfiguration configuration = (FileSharerConfiguration)serializer.Deserialize(stream);
                return configuration;
            }
        }

        private SharedFileSetCollection m_SharedFileSets;

        public SharedFileSetCollection SharedFileSets
        {
            get { return this.m_SharedFileSets; }
            set { this.m_SharedFileSets = value; }
        }


        public SharedFileSet Find(string serverItemPath)
        {
            // This could possibly be optimised/indexed if there
            // are a lot of shared files. Doing this because I
            // only have an hour or so to finish.

            foreach (SharedFileSet set in this.SharedFileSets)
            {
                foreach (SharedFileReference reference in set.SharedFileReferences)
                {
                    if (reference.Path == serverItemPath) return set;
                }
            }

            return null;
        }
    }
}
